// 音乐列表
<template>
  <div class="music-list" v-if="list.length > 0">
    <div
      class="music-item"
      v-for="(item, index) in list"
      :key="index"
      @click="selectItem(index, item)"
      :class="currentMusic.id === item.id ? 'music-item-back' : ''"
    >
      <div class="first">
        <p class="number">{{ index + 1 }}</p>
        <h4 class="music-name">{{ item.name }}</h4>
        <p class="dt">{{ item.time|format }}</p>
        <span class="like"><i class="iconfont icon-heart-off"></i></span>
      </div>
      <div class="second">
        <p class="singer">演唱：{{ item.songer }}</p>
        <p class="album" :title="item.album">专辑：{{ item.album }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import { getSimiSong } from '@/api';
import { mapMutations, mapGetters } from 'vuex';
import { format } from '@/util/util';

export default {
  name: 'music-list',
  props: {
    list: Array,
    default: () => [],
  },
  filters: {
    format,
  },
  computed: {
    ...mapGetters(['currentMusic']),
  },
  methods: {
    async selectItem(index, item) {
      // 获取推荐音乐
      const simiList = await getSimiSong(item.id);
      this.setSimiList(simiList);
      this.$emit('select', item, index);
    },
    ...mapMutations({
      setSimiList: 'SET_SIMILARLIST',
    }),
  },
};
</script>

<style lang="scss" scoped>
.music-list {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  height: 100%;
  overflow: auto;
  .music-item:nth-child(2n-1) {
    margin-right: 5%;
  }
  .music-item-back{
    background: linear-gradient(to right, rgb(184, 111, 87), rgb(187, 67, 93));
    border-radius: 5px;
  }
  .music-item {
    border-bottom: 1px solid rgb(58, 61, 102);
    width: 45%;
    box-sizing: border-box;
    padding: 10px;
    margin-bottom: 10px;
    cursor: pointer;
    .first,
    .second {
      display: flex;
    }
    .first {
      color: #ffffff;
      position: relative;
      padding-bottom: 10px;
      .music-name {
        padding-left: 15px;
        font-size: 16px;
        @include no-wrap;
        width: 80%;
      }
      .dt {
        position: absolute;
        right: 30px;
        top: 50%;
        margin-top: -15px;
      }
      .like {
        position: absolute;
        font-size: 16px;
        right: 0;
        color: #ffffff;
      }
    }
    .second {
      padding-left: 20px;
      color: rgba(203, 196, 111, 0.7);
      .singer {
        padding-right: 15px;
        @include no-wrap;
        flex: 1;
      }
      .album {
        width: 150px;
        @include no-wrap;
      }
    }
  }
}
</style>
